const path = require('path');
const packageName = require('./package.json').name;

function resolve(dir) {
    return path.join(__dirname, dir);
}
const port = 31786; // dev port
const dev = process.env.NODE_ENV === 'development'

var NODE_ENV = process.env.NODE_ENV
var publicPath = `//localhost:${port}`

// 项目名称
var proejctName = 'web-microApp-module-vue3';
var ossUrl = 'https://gstor-default-workspace-cn-shanghai-c218770b.oss-accelerate.aliyuncs.com';
var ossPath = `${ossUrl}/${proejctName}/dist/`;

// 生产
if(NODE_ENV == 'production'){
  publicPath = ossPath
}


module.exports = {
    /**
     * You will need to set publicPath if you plan to deploy your site under a sub path,
     * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
     * then publicPath should be set to "/bar/".
     * In most cases please use '/' !!!
     * Detail: https://cli.vuejs.org/config/#publicpath
     */
    publicPath: publicPath,
    outputDir: 'dist',
    chainWebpack: config => {
        // 删除 浏览器在⻚⾯加载完成后，利⽤空闲时间提前获取⽤户未来可能会访问的内容。
        config.plugins.delete('prefetch');
        // 压缩代码
        config.optimization.minimize(true);
        config.module.rule('images')
            .use('url-loader')
            .loader('url-loader')
            .options({
                limit: 4096, // ⼩于4kb将会被打包成 base64
                fallback: {
                    loader: 'file-loader',
                    options: {
                        name: './img/[name].[hash:8].[ext]',
                        publicPath: publicPath
                    }
                }
            })
        config.module
            .rule('md')
            .test(/\.md/)
            .use('text-loader')
            .loader('text-loader')
            .end()
    },
    assetsDir: 'static',
    filenameHashing: true,
    // tweak internal webpack configuration.
    // see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
    devServer: {
        // host: '0.0.0.0',
        hot: true,
        disableHostCheck: true,
        port,
        overlay: {
            warnings: false,
            errors: true,
        },
        headers: {
            'Access-Control-Allow-Origin': '*',
        },
    },
    // 自定义webpack配置 packageName的值为package.json中name值
    configureWebpack: {
        resolve: {
            alias: {
                '@': resolve('src'),
            },
        },
        output: {
        // 该值需要与主项目中micfo/apps.js apps中的name值相对应
            library: `${packageName}-name`,
            libraryTarget: 'umd',
            jsonpFunction: `webpackJsonp_${packageName}`,
        },
    },
};